<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
    
   function init() {
        if(!isset(Yii::app()->session['user_id']) || !Yii::app()->session['user_id'])
	    {
	    	//$this->redirect(Yii::app()->baseUrl.DIRECTORY_SEPARATOR.'login');
	    }
	}

    
    function performAjaxValidation($model, $form_name = '')
	{
        if(isset($_POST['ajax']) && $_POST['ajax']===$form_name)
	    {
            echo CActiveForm::validate($model);
	        Yii::app()->end();
	    }
	}
    
    public function handleAjaxValidation($model) {
        if (isset($_POST['ajax'])) {
            echo $_POST['ajax'];
            Yii::app()->end();
        }
    }
    
    /**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}
    
    public function actionAjaxUpdate($field, $value)
    {
        $model=$this->loadModel();
        $model->setAttribute($field,$value);
        $model->update();
    }
}